package com.sad.jetpack.v1.thememanager.api;

import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONObject;

public class LocalTheme {
    private int id=0;
    private BasicThemeDes basicThemeDes;
    private ThemeExtension extension;
    private boolean isDefault=false;
    private int version=0;
    public LocalTheme(){}
    public LocalTheme(int id, BasicThemeDes basicThemeDes, ThemeExtension extension, boolean isDefault, int version) {
        this.id = id;
        this.basicThemeDes = basicThemeDes;
        this.extension = extension;
        this.isDefault = isDefault;
        this.version = version;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BasicThemeDes getBasicThemeDes() {
        return basicThemeDes;
    }

    public void setBasicThemeDes(BasicThemeDes basicThemeDes) {
        this.basicThemeDes = basicThemeDes;
    }

    public ThemeExtension getExtension() {
        return extension;
    }

    public void setExtension(ThemeExtension extension) {
        this.extension = extension;
    }

    public boolean isDefault() {
        return isDefault;
    }

    public void setDefault(boolean aDefault) {
        isDefault = aDefault;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public JSONObject toJson() throws Exception{
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",id);
        jsonObject.put("version",version);
        jsonObject.put("isDefault",isDefault);
        jsonObject.put("basicThemeDes",basicThemeDes.toJson());
        jsonObject.put("extension",extension.toJson());
        return jsonObject;
    }

    public static LocalTheme fromJson(JSONObject jsonObject){
        LocalTheme localTheme=new LocalTheme();
        localTheme.setId(jsonObject.optInt("id"));
        localTheme.setDefault(jsonObject.optBoolean("isDefault",false));
        localTheme.setVersion(jsonObject.optInt("version"));
        localTheme.setBasicThemeDes(BasicThemeDes.fromJson(jsonObject.optJSONObject("basicThemeDes")));
        localTheme.setExtension(ThemeExtension.fromJson(jsonObject.optJSONObject("extension")));
        return localTheme;
    }

    private final static String THEME_MANAGER_API="THEME_MANAGER_API";
    private final static String KEY_CURRENT_THEME="current_theme";
    public static void saveCurrLocalTheme(Context context, LocalTheme theme){
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(THEME_MANAGER_API, Context.MODE_PRIVATE|Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(KEY_CURRENT_THEME, theme.toJson().toString());
            editor.commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static LocalTheme getLastLocalTheme(Context context){
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(THEME_MANAGER_API, Context.MODE_PRIVATE|Context.MODE_MULTI_PROCESS);
            String s=sharedPreferences.getString(KEY_CURRENT_THEME,"");
            return LocalTheme.fromJson(new JSONObject(s));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static LocalTheme DEFAULT_LOCAL_THEME;

}
